package com.itheima.day14.threadpool.test4;

import java.util.concurrent.*;

// ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(核心线程数量,最大线程数量,空闲线程最大存活时间,任务队列,创建线程工厂,任务的拒绝策略);
//        ThreadPoolExecutor.AbortPolicy: 		    丢弃任务并抛出RejectedExecutionException异常。是默认的策略。
//        ThreadPoolExecutor.DiscardPolicy： 		丢弃任务，但是不抛出异常 这是不推荐的做法。
//        ThreadPoolExecutor.DiscardOldestPolicy：  抛弃队列中等待最久的任务 然后把当前任务加入队列中。
//        ThreadPoolExecutor.CallerRunsPolicy:      调用任务的run()方法绕过线程池直接执行。
public class Test {
    public static void main(String[] args) {
        // ThreadPoolExecutor.AbortPolicy:          丢弃任务并抛出RejectedExecutionException异常。 是默认策略。
        // method1();

        // ThreadPoolExecutor.DiscardPolicy： 	    丢弃任务，但是不抛出异常 这是不推荐的做法。
        // method2();

        // ThreadPoolExecutor.DiscardOldestPolicy： 抛弃队列中等待最久的任务 然后把当前任务加入队列中。
        // method3();

        // ThreadPoolExecutor.CallerRunsPolicy:     调用任务的run()方法绕过线程池直接执行。
        // method4();

    }

    // ThreadPoolExecutor.CallerRunsPolicy
    private static void method4() {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2,
                5,
                2, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(5),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy());

        for (int i = 1; i <= 15; i++) {
            int count = i;
            threadPoolExecutor.submit(()->{
                System.out.println(Thread.currentThread().getName() + "正在执行" + "---" + count);
            });
        }

        //关闭线程池
        threadPoolExecutor.shutdown();
    }

    // // ThreadPoolExecutor.DiscardOldestPolicy
    private static void method3() {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(5,
                5,
                2, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(5),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardOldestPolicy());
        for (int i = 1; i <= 15; i++) {
            int count = i;
            threadPoolExecutor.submit(()->{
                System.out.println(Thread.currentThread().getName() + "正在执行" + "---" + count);
            });
        }

        // 关闭线程流
        threadPoolExecutor.shutdown();
    }

    // ThreadPoolExecutor.DiscardPolicy
    private static void method2() {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2,
                5,
                2, TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(5),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.DiscardPolicy());

        for (int i = 1; i <= 11; i++) {
            threadPoolExecutor.submit(new MyRubbable());
        }

        // 关闭线程池
        threadPoolExecutor.shutdown();
    }

    // ThreadPoolExecutor.AbortPolicy
    private static void method1() {
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2,
                5,
                2,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(5),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy());

        try {
            for (int i = 1; i <= 11; i++) {
                threadPoolExecutor.submit(new MyRubbable());
            }
        } catch (RejectedExecutionException e) {
            System.out.println("超过最大可执行任务数");
        }

        // 关闭线程池
        threadPoolExecutor.shutdown();
    }
}
